/* Note: 	bank_reg_final.do runs bank-level regressions.
		
*  Input: 	reg_full_final.dta			// Data set up for regression 
			
*  Plots:   Coeff_plot.eps

*  Tables:  bank_reg_PL_avg.tex			// Table 5 a
			bank_reg_PL_wavg.tex		// Table 5 b
			bank_summary_stat.tex 		// Table 2
*/


clear all

grstyle init
grstyle set plain, horizontal
grstyle set color Set1


********************************************************************************
*	Bank-level summary stat (Table 2)
********************************************************************************

use "./Data/reg_full_final", clear
keep if mdate==tm(2009m12) 

rename binding Binding

g DPTARatio=Deriv_pos/AssetsUSDk
g CTARatio=CapitalUSDk/AssetsUSDk

g DerivPosition_mio= Deriv_pos/1000
g Capital_mio= CapitalUSDk/1000
g Assets_mio= AssetsUSDk/1000


g DPTCRatio_inv= 1/DPTCRatio
g DPTARatio_pct= DPTARatio*100
g LTARatio_pct= LTARatio*100
g CTARatio_pct= CTARatio*100
g DepositRatio_pct= DepositRatio*100
g Leverage_pct= Leverage*100
g FCYLoansRatio_pct= FCYLoansRatio*100
g FCYLiabRatio_pct= FCYLiabRatio*100

eststo full2: quietly estpost summarize DerivPosition_mio Capital_mio Assets_mio DPTARatio_pct LTARatio_pct DepositRatio_pct Leverage_pct FCYLoansRatio_pct FCYLiabRatio_pct if Binding==1 | Binding==0
eststo bind2: quietly estpost summarize DerivPosition_mio Capital_mio Assets_mio DPTARatio_pct LTARatio_pct DepositRatio_pct Leverage_pct FCYLoansRatio_pct FCYLiabRatio_pct if Binding==1
eststo nobind2: quietly estpost summarize DerivPosition_mio Capital_mio Assets_mio DPTARatio_pct LTARatio_pct DepositRatio_pct Leverage_pct FCYLoansRatio_pct FCYLiabRatio_pct if Binding==0
eststo diff2: quietly estpost ttest DerivPosition_mio Capital_mio Assets_mio DPTARatio_pct LTARatio_pct DepositRatio_pct Leverage_pct FCYLoansRatio_pct FCYLiabRatio_pct, by(Binding) unequal
label var DPTCRatio_inv "Capital/FXD"
label var DPTARatio_pct "FXD/Assets (\%)"
label var CTARatio_pct "Capital/Assets (\%)"
label var LTARatio_pct "Loans/Assets (\%)"
label var DepositRatio_pct "Deposits/Assets (\%)"
label var Leverage_pct "Equity/Assets (\%)"
label var FCYLoansRatio_pct "FC Loan Share (\%)"
label var FCYLiabRatio_pct "FC Liab Share (\%)"
local fn="./Output/Table_2_bank_summary_stat.tex"
esttab full2 bind2 nobind2 diff2, mtitles("Full Sample" "Constrained" "Unconstrained" "Diff") cells("mean(pattern(1 1 1 0) fmt(%12.0fc)) sd(pattern(1 1 1 0) fmt(%12.0fc)) b(star pattern(0 0 0 1) fmt(%12.0fc)) t(pattern(0 0 0 1) par fmt(1))") alignment(r) replace nonumber label
esttab full2 bind2 nobind2 diff2 using `fn', mtitles("Full Sample" "Constrained" "Unconstrained" "Difference") cells("mean(pattern(1 1 1 0) fmt(%12.0fc)) sd(pattern(1 1 1 0) fmt(%12.0fc)) b(star pattern(0 0 0 1) fmt(%12.0fc)) t(pattern(0 0 0 1) par fmt(1))") replace nonumber label





********************************************************************************
*	Bank-level regression (Table 5)
********************************************************************************

foreach const in "PL_avg" "PL_wavg"{
use "./Data/reg_full_final", clear
g constraint= `const'

local i=1

sort mdate

label var constraint "Regulation"

 
g Dit= binding *constraint


label var Dit "Constrained=1 x Regulation"

tempfile temp 
save `temp', replace


foreach variable in "Deriv_pos_abs_log" "Capital_log" {
use `temp', clear
g yvar=`variable'
sort mdate


qui reg yvar i.binding i.mdate Dit, vce(cluster bank)

eststo m`i'
local i=`i'+1
estadd local BankFE "N"
estadd local TimeFE "Y"

 
qui reg yvar i.mdate Dit i.bankid, vce(cluster bank)
eststo m`i'
local i=`i'+1
estadd local BankFE "Y"
estadd local TimeFE "Y"


}

esttab m1 m2 m3 m4, s(BankFE TimeFE N r2, label("Bank FE" "Time FE" "N" "Adj RSqr")) ar2 mtitle("LogFXD" "LogFXD" "LogCapital" "LogCapital" "FXD/Capital" "FXD/Capital") label star(* 0.10 ** 0.05 *** 0.01) keep(1.binding Dit ) order(Dit)  replace

local fn="./Output/Table_5_bank_reg_`const'.tex"
esttab m1 m2 m3 m4 using `fn', s(BankFE TimeFE N r2, label("BankFE" "TimeFE" "N" "Adj RSqr")) ar2 mtitle("LogFXD" "LogFXD" "LogCapital" "LogCapital" "FXD/Capital" "FXD/Capital") label star(* 0.10 ** 0.05 *** 0.01) keep(1.binding Dit ) order(Dit) replace

}





********************************************************************************
*	Bank-level DiD Coefficient Plot (Figure 8)
********************************************************************************

use"./Data/reg_full_final", clear  
g Year=year(Date)
g Post=0
replace Post=1 if mdate>=tm(2010m7)
g qdate=qofd(Date)
format qdate %tq

tempfile reg_full_yearly
save `reg_full_yearly', replace 

sort mdate


use `reg_full_yearly', clear
drop if CB==1 // Exclude closed banks to minimize data noise as they approach closure. A plot without this filter is reported in the internet appendix.
local i=1

sort mdate

forval md=576/707{
g I_`md'=0
replace I_`md'=1 if mdate==`md'
g D_`md'=0
replace D_`md'=I_`md' * binding
}
 
g yvar=Deriv_pos_abs_log
sort mdate

drop D_576
 
reg yvar i.binding i.Post D_*, vce(cluster bank)  

*Coefficient plot
estimates store coeffs
coefplot (coeffs, /// 
					recast(connected) lcolor(navy) msymbol(none) lwidth(medthick) ///
						ciopts(lcolor(navy) lwidth(vvthin)) ///
		  ), ///
		  keep(D_*) vertical yline(0, lcolor(navy)) title("Coefficient by Month") /// 
		  xline(29 40 58) xline(101, lcolor(gray%50)) coeflabels(D_*=" " ) ///
		  xlabel(1 "2008 Jan" 29 "2010 Jun" 40 "2011 May" 58 "2012 Nov" 101 "2016 Jun" 131 "2018 Dec", angle(45))
*graph export "./Output/Figure_8_Coeff_plot.eps", as(eps) preview(off) replace
graph export "./Output/Figure_8_Coeff_plot.pdf", as(pdf) replace
